﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class SearchNews : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "SearchNews";
            }
        }

        public override bool RunStandalone
        {
            get
            {
                return true;
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Tìm kiếm tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[]{
                    new AddNews(),new UpdateNews(),new NewsPreview(),new UploadImage(),new UpdateRelationNews(),new NewsUploadDocument(),new MoveNews()
                };
                return SubWebParts;
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            string ActiveSite = Portal.Environment.Environments.GetActiveSite();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(SearchNews));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "SearchNews.js");

            string SelectSite =
                  "<select id=drpSelectSiteForSearchNews onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(ActiveSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";


                string SelectWorkFlowStatusText =
                    "<select id=drpSelectWorkflowStatus>" +
                    "   <option value=\"0\">" + ResourceBLL.GetText(WebPartId, "NotPublishingComboxItem", "Chưa phát hành") + "</option>" +
                    "   <option value=\"1\">" + ResourceBLL.GetText(WebPartId, "PublishingComboxItem", "Đã phát hành") + "</option>" +
                    "</select>";

            RetHtml+=
                    "<table>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "Site", "Site") + "</td>" +
                    "       <td>" + SelectSite + "</td>" +
                    "   </tr>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + "</td>" +
                    "       <td><input class=cssSearchNewsKeyword id=txtSearchKeyword value=\"\"></td>" +
                    "   </tr>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "Time", "Thời gian") + "</td>" +
                    "       <td>" + Environment.LookUpUtilities.GenerateLookUpDate("txtFromDate", "") + " ~ " + Environment.LookUpUtilities.GenerateLookUpDate("txtToDate", "") + "</td>" +
                    "   </tr>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "Source", "Nguồn") + "</td>" +
                    "       <td><input id=txtSource value=\"\"></td>" +
                    "   </tr>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "GroupNewsItem", "Nhóm") + "</td>" +
                    "       <td><div id=divSelectGroupNewsItem>" + ServerSideGetGroupNewsItem(ActiveSite, "") + "</div></td>" +
                    "   </tr>" +
                    "   <tr>" +
                    "       <td>" + ResourceBLL.GetText(WebPartId, "Status", "Trạng thái") + "</td>" +
                    "       <td>" + SelectWorkFlowStatusText + "</td>" +
                    "   </tr>" +
                      "   <tr>" +
                    "       <td></td>" +
                    "       <td><input type=button onclick=javascript:SearchNewsJumpPage(); value=\"" + ResourceBLL.GetText(WebPartId, "Search", "Tìm kiếm") + "\"></td>" +
                    "   </tr>" +
                    "</table>" +
                    "<div id=divProcessing></div>"+
                    "<div id=divSearchNewsContent></div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(
            string belongSite,
            string Keyword,
            string fromDate,
            string toDate,
            int WorkflowStatusIndex, 
            string SGroupNewsItemId,
            string Source,
            int    PageIndex)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                //if(Keyword.Trim().Length==0 && Source.Trim().Length==0)throw new Exception(ResourceBLL.GetText(WebPartId,"KeywordIsMissing","Chưa nhập từ khóa tìm kiếm..."));
                string RetHtml = "";
                Keyword = Environment.WebPartUtilities.CorrectForSearch(Keyword);
                Source = Environment.WebPartUtilities.CorrectForSearch(Source);
                fromDate = Environment.WebPartUtilities.CorrectForSearch(fromDate);
                toDate = Environment.WebPartUtilities.CorrectForSearch(toDate);

                DataTable dtNews = new NewsBLL().Searching(belongSite, Keyword, fromDate, toDate, WorkflowStatusIndex, Source, SGroupNewsItemId);
                int ItemPerPage = 20;
                int MaxPage = dtNews.Rows.Count / ItemPerPage;
                if (dtNews.Rows.Count % ItemPerPage != 0) MaxPage++;
                int StartIndex = PageIndex * ItemPerPage;
                int EndIndex = StartIndex + ItemPerPage;
                if (EndIndex > dtNews.Rows.Count) EndIndex = dtNews.Rows.Count;

                string SelectPageText =
                    ResourceBLL.GetText(WebPartId, "PageText", "Trang") + ": <select id=divSelectSearchNewsPage>";
                for (int iIndex = 0; iIndex < MaxPage; iIndex++)
                {
                    if (PageIndex == iIndex)
                    {
                        SelectPageText += "<option selected>" + (iIndex + 1) + "</option>";
                    }
                    else
                    {
                        SelectPageText += "<option>" + (iIndex + 1) + "</option>";
                    }
                }
                SelectPageText += "</select><input type=button value=\"Go\" onclick=javascript:SearchNewsJumpPage();>";
                if (MaxPage <= 1) SelectPageText = "";

                
                if (dtNews.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=SelectPageText+
                    "<table class=cssNewsContent>" +
                           "  <tr>" +
                           "      <th class=cssCol0>#</th>" +
                           "      <th class=cssCol1>" + ResourceBLL.GetText(WebPartId, "SubjectText", "Tiêu đề") + "</th>" +
                           "      <th class=cssCol3></th>" +
                           "      <th class=cssCol4></th>" +
                           "      <th class=cssCol5></th>" +
                           "      <th class=cssCol6></th>" +
                           "      <th class=cssCol7></th>" +
                           " </tr>";
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string NewsId = (string)dtNews.Rows[iIndex]["NewsId"];
                        string GroupNewsId = (string)dtNews.Rows[iIndex]["GroupNewsId"];
                        string GroupNewsItemId = (string)dtNews.Rows[iIndex]["GroupNewsItemId"];
                        string SubjectText = "";
                        try
                        {
                            SubjectText = (string)dtNews.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                        }
                        catch { }
                        int IsPublishing = 0;
                        try
                        {
                            IsPublishing = (int)dtNews.Rows[iIndex]["IsPublishing"];
                        }
                        catch { }
                        DateTime EntryDate = (DateTime)dtNews.Rows[iIndex]["EntryDate"];
                        string tWorkFlowStatus = "";
                        try
                        {
                            tWorkFlowStatus = (string)dtNews.Rows[iIndex]["WorkFlowStatus"];
                        }
                        catch { }

                        string PublishingUrl = "<a href=\"javascript:PublishingStdAloneNews('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "Publishing", "(Xuất bản)") + "</a>";
                        if (IsPublishing == 1)
                        {
                            PublishingUrl = "<a href=\"javascript:UnPublishingStdAloneNews('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "RemoveFromRemoteServer", "(Ngừng)") + "</a>";
                        }


                        string ImageUrl = "<a href=\"javascript:ClientUploadImageUrl('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "UploadImage", "(Image)") + "</a>";

                        int ShowOnTop = 0;
                        try
                        {
                            ShowOnTop = (int)dtNews.Rows[iIndex]["ShowOnTop"];
                        }
                        catch { }
                        string ShowOnTopUrl = "<a href=\"javascript:ClientSetShowOnTop('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "ShowOnTop", "(OnTop)") + "</a>";
                        if (ShowOnTop == 1)
                        {
                            ShowOnTopUrl = "";
                        }
                        if (IsPublishing == 0) ShowOnTopUrl = "(n/a)";

                        string MoveItemUrl = "<a href=\"javascript:ClientMoveItemNews('" + belongSite + "','" + GroupNewsItemId + "','" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "Move", "(chuyển)") + "</a>";
                        string DocumentItemUrl = "<a href=\"javascript:ClientUploadDocumentContent('" + belongSite + "','" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "Upload", "(upload)") + "</a>";

                        string tbelongSite = (string)dtNews.Rows[iIndex]["belongSite"];
                        string ShortId = (string)dtNews.Rows[iIndex]["ShortId"];
                        //string LinkText = Environments.GetNewsUrl(tbelongSite, ShortId, SubjectText, "_runtime");
                        string NewsUrl = Environments.GetNewsUrl(belongSite, ShortId, SubjectText, "_runtime");
                        NewsUrl = "<a target=_blank href=\""+NewsUrl+"\">" + NewsUrl + "</a>";
                        //LinkText = LinkText.Substring(LinkText.LastIndexOf("/") + 1);
                        //if (IsPublishing == 1)
                        //{
                        //    LinkText = "<a target=\"_blank\" href=\"" + Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + LinkText + "\">" + LinkText + "</a>";
                        //}

                        int TotalNewsRelation = 0;
                        if (dtNews.Rows[iIndex]["TotalNewsRelation"] != DBNull.Value)
                        {
                            TotalNewsRelation = (int)dtNews.Rows[iIndex]["TotalNewsRelation"];
                        }
                        string UploadFileName = "";
                        if (dtNews.Rows[iIndex]["UploadFileName"] != DBNull.Value)
                        {
                            UploadFileName = (string)dtNews.Rows[iIndex]["UploadFileName"];
                        }
                        string DownloadText = "";
                        if (UploadFileName.Trim().Length > 0)
                        {
                            string Url = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/News/Archives/" + belongSite + "/" + UploadFileName;
                            DownloadText += "<a href=\"" + Url + "\">(download)</a>";
                        }

                        string RelationNewsText = "<a href=\"javascript:ClientShowRelationNewsFormSearchNews('" + NewsId + "');\">" + GetText("RelationNewsText", "Tin liên quan") +" ("+TotalNewsRelation.ToString("#,##0")+")</a><br>";

                        RetHtml +=
                               "  <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                               "      <td valign=top>" + (iIndex + 1) + "</td>" +
                               "      <td valign=top class=cssTdSubjectText>" + SubjectText + "<br><span class=cssLinkNews>" + NewsUrl + "</span></td>" +
                               "      <td valign=top nowrap><a href=\"javascript:ShowUpdateStdAloneNewsForm('" + GroupNewsId + "','" + GroupNewsItemId+ "','" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "UpdateText", "Cập nhật") + "</a></td>" +
                               "      <td valign=top><a href=\"javascript:ClientDeleteStdAloneNews('" + GroupNewsId + "','" + GroupNewsItemId + "','" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "DeleteText", "Xóa") + "</a></td>" +
                               "      <td valign=top>" + tWorkFlowStatus + "</td>" +
                               "      <td valign=top>" + PublishingUrl + "</td>" +
                               "      <td valign=top>" + ImageUrl + "<br>" + ShowOnTopUrl + "<br><br>" + MoveItemUrl + "<br>" + DocumentItemUrl + "<br>" + DownloadText + "<br><a href=\"javascript:ShowNewsHistory('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "Log", "Log") + "</a><br>"+RelationNewsText+"<br><a href=\"javascript:ClientSyncNews('" + NewsId + "');\">" + ResourceBLL.GetText(WebPartId, "SyncNews", "Sync") + "</a></td>" +
                               " </tr>";
                    }
                    RetHtml += "</table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: "+ex.Message.ToString();
            }
        }

        
        [AjaxPro.AjaxMethod]
        public string GetConfirmMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingNewsMsg()
        {
            return ResourceBLL.GetText(WebPartId, "GetConfirmPublishingNewsMsg", "Bạn đã chắc chắn xuất bản lên web (public) chưa?");
        }



        [AjaxPro.AjaxMethod]
        public string GetSearchProcessingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "SearchProcessingMsg", "Đang tìm kiếm! Xin vui lòng chờ...");
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmUnPublishingNewsMsg()
        {
            return ResourceBLL.GetText(WebPartId, "GetConfirmUnPublishingNewsMsg", "Bạn đã chắc chắn ngừng xuất bản lên web (public) chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetDeleteOkMsg()
        {
            return ResourceBLL.GetText(WebPartId,"DeleteOk","Xóa thành công");
        }

        [AjaxPro.AjaxMethod]
        public string CheckAllowDelete(string NewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                                           new GroupNews().PermissionTypes, "GroupNewsItems");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                if (NewsCls == null) throw new Exception(GetText("HasDeleted", "Bản tin đã được xóa!"));
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, NewsCls.GroupNewsItemId);
                Environment.Permission.CheckPermission(
                    Environment.Environments.CurrentLoginUser.LoginName,
                    PermissionTypeCls.PermissionTypeCode,
                    "DeleteNewsItem", GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.Owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteNews(string NewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                             new GroupNews().PermissionTypes, "GroupNewsItems");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                if (NewsCls == null) throw new Exception(GetText("HasDeleted", "Bản tin đã được xóa!"));
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, NewsCls.GroupNewsItemId);
                Environment.Permission.CheckPermission(
                    Environment.Environments.CurrentLoginUser.LoginName,
                    PermissionTypeCls.PermissionTypeCode,
                    "DeleteNewsItem", GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.Owner, true);


                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new NewsBLL().DeleteNews(sqlTransaction, NewsId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUpdateNewsUrl(string GroupNewsId, string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                    new GroupNews().PermissionTypes, "GroupNewsItems");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                if (NewsCls == null) throw new Exception(GetText("HasDeleted", "Bản tin đã được xóa!"));
                GroupNewsItemCls GroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, NewsCls.GroupNewsItemId);
                Environment.Permission.CheckPermission(
                    Environment.Environments.CurrentLoginUser.LoginName,
                    PermissionTypeCls.PermissionTypeCode,
                    "UpdateNewsItem", GroupNewsItemCls.GroupNewsItemId, GroupNewsItemCls.Owner, true);


                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateNews().WebPartId),
                        new ParamCls("GroupNewsId",GroupNewsId),
                        new ParamCls("NewsId",NewsId)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string GetUpdateImageUrl(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadImage().WebPartId),
                        new ParamCls("NewsId",NewsId)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSidePublishingNews(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                //PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new i FindPermissionType("News");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PublishingNews", NewsId, NewsCls.Owner, true);
   
                new NewsBLL().PublishingPublicWebSite(NewsId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Xuất bản tin lên web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideUnPublishingNews(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                //PermissionTypeCls PermissionTypeCls = FindPermissionType("News");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PublishingNews", NewsId, NewsCls.Owner, true);

                new NewsBLL().UnPublishingPublicWebSite(NewsId);
                return ResourceBLL.GetText(WebPartId, "UnPublishingOk", "Ngừng xuất bản tin lên web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string CheckAllowPublishingNews(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                //PermissionTypeCls PermissionTypeCls = FindPermissionType("News");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PublishingNews", NewsId, NewsCls.Owner, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermission(string NewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                //PermissionTypeCls PermissionTypeCls = FindPermissionType("News");
                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionNews", NewsId, NewsCls.Owner, true);
                return Environment.Permission.GetPermissionUrl(WebPartId, NewsId, "News");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideGetGroupNewsItem(string belongSite, string SGroupNewsItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                string SelectGroupNewsText =
                   "<select id=drpSelectGroupNewsItem>";
                DataTable dtGroupNewsItems = new GroupNewsBLL().ReadingGroupNewsItemsForSearch(belongSite);
                for (int iIndex = 0; iIndex < dtGroupNewsItems.Rows.Count; iIndex++)
                {
                    string GroupNewsItemName = (string)dtGroupNewsItems.Rows[iIndex][Environments.ActiveLanguage + "GroupNewsItemName"];
                    string GroupNewsItemId = (string)dtGroupNewsItems.Rows[iIndex]["GroupNewsItemId"];
                    if (GroupNewsItemId.Equals(SGroupNewsItemId))
                    {
                        SelectGroupNewsText += "<option selected value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                    else
                    {
                        SelectGroupNewsText += "<option  value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
                    }
                }
                SelectGroupNewsText += "</select>";
                if (dtGroupNewsItems.Rows.Count == 0) SelectGroupNewsText = "?";
                return SelectGroupNewsText;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string GetLogUrl(string NewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                string LogUrl = Screen.BuildDialogUrl(new NewsHistory().WebPartId, new ParamCls[]{
                        new ParamCls("NewsId",NewsId)
                    });
                return LogUrl;

            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSetShowOnTop(string NewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                            new GroupNews().PermissionTypes, "GroupNewsItems");


                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateNews", NewsId, NewsCls.Owner, true);
                new NewsBLL().SetOnTop(null, NewsCls.GroupNewsItemId, NewsId);
                return ResourceBLL.GetText(WebPartId, "SetOnTopOk", "Đặt hiển thị đầu tiên thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSyncToServer(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new NewsBLL().SyncNews(NewsId);
                return ResourceBLL.GetText(WebPartId, "SyncToServer", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }



        [AjaxPro.AjaxMethod]
        public string GetSyncProcessing()
        {
            return ResourceBLL.GetText(WebPartId, "SyncProcessing", "Đang cập dữ liệu cho máy chủ public...");
        }


        [AjaxPro.AjaxMethod]
        public string GetUploadDocumentContentUrl(string belongSite, string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(
                            new GroupNews().PermissionTypes, "GroupNewsItems");

                NewsCls NewsCls = new NewsBLL().CreateModel(Environments.ActiveLanguage, NewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateNews", NewsId, NewsCls.Owner, true);

                return Screen.BuildDialogUrl(new NewsUploadDocument().WebPartId, new ParamCls[]{
                    new ParamCls("belongSite",belongSite),
                    new ParamCls("NewsId",NewsId)
                });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetMoveNewsUrl(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new MoveNews().WebPartId),
                        new ParamCls("NewsId",NewsId)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetRelationFormUrl(string NewsId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateRelationNews().WebPartId),
                        new ParamCls("NewsId",NewsId)
                    });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}
